<?php

namespace app\index\controller;
use app\common\func\allRequest;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use think\Cookie;

class Index extends Controller
{
     public function _initialize(){
        if (Request::instance()->isMobile()) 
        return $this->redirect('mobile/Index/index');
    }
        
    public function index()
    {

        $res =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_am sham', 'sham.mid  = shm.id')
                ->join('sm_h_actor sha', 'sha.id = sham.aid')
                ->field('shm.mname,sha.name,shm.id')
                ->select();
        $data = Db::table('sm_h_movie')
                ->limit(15)
                ->order('id','desc')
                ->select();
        $rank = Db::table('sm_h_movie')
                ->limit(10)
                ->order('score','desc')
                ->field('mname,id')
                ->select();
                // var_dump($rank);die;
        $hot = Db::table('sm_h_movie')
                ->where('score','>=','4')
                ->limit(15)
                ->select();
        $user = Db::table('sm_h_user')
                ->field('name')
                ->where('id', Session::get('id'))
                ->find();
        $url = "http://119.23.56.18/api";
        $urlreq = new allRequest();
        $msg = $urlreq->curl_request($url);
        $arr = json_decode($msg,true);

         // 电影票
            $url = "http://v.juhe.cn/wepiao/query?key=b6e2cd3eaab2bb865b58216448f7b3eb";
            $urlreq = new allRequest();
            $msg = $urlreq->curl_request($url);
            $ggg = json_decode($msg,true);
            // dump($ggg['result']['h5url']);die;

         if(Cookie::get('id')){
            $email = Cookie::get('email');
            $phone = Cookie::get('phone');
            $pwd = Cookie::get('password');
            $ckget = [
                'email'=>$email,
                'phone'=>$phone,
                'password'=>$pwd
            ];
            $cook = db('h_user')->where($ckget)->find();
           //            var_dump($res);die;
            if($cook){
                Session::set('id', $cook['id']);
                Session::set('email', $cook['email']);
                Session::set('name', $cook['name']);
                Session::set('phone', $cook['phone']);
                Session::set('password', $cook['password']);
                return view('index@Index/index', [
                    'res' => $res,
                    'user'=>$user,
                    'data' => $data,
                    'hot' => $hot,
                    'arr' => $arr,
                    'rank' => $rank,
                    'ggg' => $ggg
                ]);
            }else{
                return $this->error('自动登陆异常，请重新登陆', url('index/Login/index'));
            }
        }elseif(Session::get('id')){
            $seget = [
                'email'=>Session::get('email'),
                'phone'=>Session::get('phone'),
                'password'=>Session::get('password')
            ];
            $sess = db('h_user')->where($seget)->find();
            if(!$sess){
                return $this->error('自动登陆异常，请重新登陆', url('index/Login/index'));
            }else{
                return view('index@Index/index', [
                    'res' => $res,
                    'user'=>$user,
                    'data' => $data,
                    'hot' => $hot,
                    'arr' => $arr,
                    'rank' => $rank,
                     'ggg' => $ggg
                ]);
            }
        }else{
            return view('index@Index/index', [
                'res' => $res,
                'user'=>$user,
                'data' => $data,
                'hot' => $hot,
                'arr' => $arr,
                'rank' => $rank,
                'ggg' => $ggg
            ]);
        }

        
    }

    public function show($id)
    {
        $res = Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_am sham', 'sham.mid  = shm.id')
                ->join('sm_h_actor sha', 'sha.id = sham.aid')
                ->field('shm.mname,sha.name,shm.id')
                ->where('shm.id',$id)
                ->select();
        $data = Db::table('sm_h_movie')->find($id);
        $type = $data['type'];

        $like =  Db::table('sm_h_movie')->where('type',$type)->where('id','not in',$id)->limit(5)->select();
        $rev = Db::table('sm_h_comment')
                ->alias('shc')
                ->join('sm_h_user shu', 'shu.id = shc.uid')
                ->join('sm_h_movie shm', 'shm.id = shc.mid')
                ->field('shc.title,shc.comment,shc.time,shu.name,shu.face')
                ->where('shm.id',$id)
                ->select();
        $people = Db('h_comment')
                    ->where('mid',$id)
                    ->count('uid');
        if (empty(Session::get('id'))) {
            if(!empty($res)){
                return view('index@Movie/index', [
                'res' => $res,
                'data' => $data,
                'like' => $like,
                'rev' => $rev,
                'people' => $people,
            ]);
            }else{
                return $this->redirect('index/Fzf/index');
            }
            
        }else{
            $user = Session::get();
            $uid = $user['id'];
            $fav =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_favorite shf', 'shf.mid  = shm.id')
                ->join('sm_h_user shu', 'shu.id = shf.uid')
                ->where('shu.id',$uid)
                ->where('shm.id',$id)
                ->field('shm.id')
                ->find();
            $com =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_comment shc', 'shc.mid  = shm.id')
                ->join('sm_h_user shu', 'shu.id = shc.uid')
                ->where('shu.id',$uid)
                ->where('shm.id',$id)
                ->field('shm.id')
                ->find();
            // var_dump($people);die;
            if(!empty($res)){
                return view('index@Movie/index', [
                    'res' => $res,
                    'data' => $data,
                    'like' => $like,
                    'rev' => $rev,
                    'people' => $people,
                    'fav' => $fav,
                    'com' => $com
                ]);
            }else{
                return $this->redirect('index/Fzf/index');
            }
            
        }

    }

    public function type($id)
    {
        if ($id == 1) {
            $res =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_am sham', 'sham.mid  = shm.id')
                ->join('sm_h_actor sha', 'sha.id = sham.aid')
                ->field('shm.mname,sha.name,shm.id')
                ->where('sm_h_movie.country','中国')
                ->whereor('sm_h_movie.country','台湾')
                ->whereor('sm_h_movie.country','香港')
                ->select();

            $data = Db::table('sm_h_movie')->where('sm_h_movie.country','中国')
                ->whereor('sm_h_movie.country','台湾')
                ->whereor('sm_h_movie.country','香港')
                ->select();
                    return view('index@Fenlei/index', [
                        'res' => $res,
                        'data' => $data,
                        'title' =>'华语电影'
                    ]);
               
        }else if ($id == 2) {
            $res =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_am sham', 'sham.mid  = shm.id')
                ->join('sm_h_actor sha', 'sha.id = sham.aid')
                ->field('shm.mname,sha.name,shm.id')
                ->where('sm_h_movie.country','美国')
                ->whereor('sm_h_movie.country','英国')
                ->whereor('sm_h_movie.country','法国')
                ->whereor('sm_h_movie.country','德国')
                ->select();

            $data = Db::table('sm_h_movie')
                ->where('sm_h_movie.country','美国')
                ->whereor('sm_h_movie.country','英国')
                ->whereor('sm_h_movie.country','法国')
                ->whereor('sm_h_movie.country','德国')
                ->select();
            return view('index@Fenlei/index', [
                'res' => $res,
                'data' => $data,
                'title' =>'欧美电影'
            ]);
        }else if ($id == 3) {
            $res =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_am sham', 'sham.mid  = shm.id')
                ->join('sm_h_actor sha', 'sha.id = sham.aid')
                ->field('shm.mname,sha.name,shm.id')
                ->where('sm_h_movie.country','韩国')
                ->select();

            $data = Db::table('sm_h_movie')
                ->where('sm_h_movie.country','韩国')
                ->select();
            return view('index@Fenlei/index', [
                'res' => $res,
                'data' => $data,
                'title' =>'韩国电影'

            ]);
        }else if ($id == 4) {

            $res =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_am sham', 'sham.mid  = shm.id')
                ->join('sm_h_actor sha', 'sha.id = sham.aid')
                ->field('shm.mname,sha.name,shm.id')
                ->where('sm_h_movie.country','日本')
                ->select();
            $data = Db::table('sm_h_movie')
                ->where('sm_h_movie.country','日本')
                ->select();
            return view('index@Fenlei/index', [
                'res' => $res,
                'data' => $data,
                'title' =>'日本电影'
            ]);
        }else{
                    return $this->redirect('index/Fzf/index');
                }

    }

    public function search()
    {
        $where = [];
        $keyword  = isset($_GET['keyword'])?$_GET['keyword'] : '';
        // var_dump($keyword);die;
        if($keyword){
            $where['mname'] = ['like', '%'.$keyword.'%'];
         
         // var_dump($where);die;
         $list = Db::name('h_movie')->where($where)->order('id DESC')->paginate(5);
         // dump($list->all());die;
         $page = $list->render();
         if ($list->all()) {
         return view('index@Index/search',[
                'title' => '搜索关键字:'.$keyword,
                'list' => $list,
                'page' => $page
            ]);
         }else{
             return view('index@Index/search',[
                'title' => '搜索关键字:'.$keyword,
                'res' => '没有找到关于 ['.$keyword.'] 的电影，换个搜索词试试吧。꒰๑´•.̫ • `๑꒱',
            ]);
         }
        }else{
            return view('index@Index/search',[
                'title' => '搜索关键字:'.$keyword,
                'res' => '没有找到关于 ['.$keyword.'] 的电影，换个搜索词试试吧。..•˘_˘•..',
            ]);
        }
         
         
        }

        public function favorite($id)
{
    $user = Session::get();
    $uid = $user['id'];
    $s = [
        'uid' => $uid,
        'mid' => $id
    ];

    $result = Db('h_favorite')->insert($s);
    if ($result > 0) {
        $info['status'] = true;
    } else {
        $info['status'] = false;
    }
    return json($info);
}

public function delfav($id)
{
    $user = Session::get();
    $uid = $user['id'];
    $result = db('h_favorite')->where('mid',$id)->where('uid',$uid)->delete();
    if ($result > 0) {
        $info['status'] = true;
    } else {
        $info['status'] = false;
    }
    return json($info);

}


    public function callapi()
    {
        $url = "http://v.juhe.cn/wepiao/query";
        $urlreq = new allRequest();
        $msg = $urlreq->curl_request($url);
        dump($msg);exit;
        dump(json_decode($msg));exit;
    }
        
    public function whale()
    {
        $data = Db::name('h_user')->where('name','ttr')->field('face')->select();
        return json($data);
    }


}
